package handler

import (
	"company/api/requset"
	"company/internal/dao"
	"company/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 创建群聊
func AddGroup(c *gin.Context) {
	//接收参数
	var req requset.AddGroupReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	group := dao.Create(&model.Group{
		UserId: c.GetUint("userId"),
		Name:   req.Name,
	})
	dao.Create(&group)

	//保存群聊
	var groups model.Group
	result := dao.Create(&model.GroupUser{
		GroupId: groups.Id,
		UserId:  c.GetUint("userId"),
		Level:   1,
	})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "创建群聊失败",
			"data": false,
		})
	}

	//返回
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "创建群聊成功",
		"data": true,
	})

}

// 修改权限
func UpdateGroup(c *gin.Context) {
	//接收参数
	var req requset.UpdateGroupReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	//判断用户权限
	var user model.GroupUser
	dao.GetOneByFields(&model.GroupUser{UserId: req.UserId}, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}

	if user.Level != 1 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "不是群主，权限不足",
			"data": nil,
		})
		return
	}

	//判断群聊是否存在
	var group model.GroupUser
	dao.GetOneByFields(&model.GroupUser{
		GroupId: req.GroupId,
		UserId:  c.GetUint("userId"),
	}, &group)
	if group.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "群聊不存在",
			"data": nil,
		})
		return
	}

	//修改
	result := dao.BatchUpdate(&model.GroupUser{
		GroupId: req.GroupId,
		UserId:  req.UserId,
	}, &model.GroupUser{Level: req.Level})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "修改失败",
			"data": false,
		})
		return
	}

	//返回
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改权限成功",
		"data": true,
	})
	return

}

// 解散
func DelGroup(c *gin.Context) {
	//接收参数
	var req requset.DelGroupReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	//判断群聊是否存在
	var group model.GroupUser
	dao.GetOneByFields(&model.GroupUser{
		GroupId: req.GroupId,
	}, &group)
	if group.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "群聊不存在",
			"data": nil,
		})
		return
	}

	//判断用户权限
	var user model.GroupUser
	dao.GetOneByFields(&model.GroupUser{UserId: c.GetUint("userId")}, &user)
	if user.Level != 1 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "不是群主，权限不足",
			"data": nil,
		})
		return
	}

	//解散
	result1 := dao.Delete(&model.Group{Id: req.GroupId})
	result2 := dao.BatchDelete(&model.GroupUser{GroupId: req.GroupId})
	if !result1 || !result2 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "解散失败",
			"data": false,
		})
		return
	}

	//返回
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "解散成功",
		"data": true,
	})

}
